<template>
  <div>
    <div class="org-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>营销组织管理</el-breadcrumb-item>
        <el-breadcrumb-item>职位管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="org-container">
      <div class="org-content">
        <div class="org-header">
          <span>职位管理</span>
          <el-button type="primary" plain @click="addPosition()">添加职位</el-button>
        </div>
        <div class="position-operator">
          <div class="org-label">创建时间</div>
          <el-date-picker
            size="mini"
            v-model="dateValue"
            type="daterange"
            align="right"
            unlink-panels
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
          <div class="org-filter">
            <el-input v-model="filterStr" size="mini" placeholder="职位"></el-input>
          </div>
          <div>
            <el-button size="mini" type="primary" @click="currentPage = 1;onSearch()">搜索</el-button>
          </div>
          <div class="org-label org-right">共{{total}}条数据</div>
        </div>
        <div>
          <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
              prop="id"
              label="编号">
            </el-table-column>
            <el-table-column
              prop="positionName"
              label="职位">
            </el-table-column>
            <el-table-column
              prop="positionDescription"
              label="职位说明">
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间">
            </el-table-column>
            <el-table-column
              prop="updateTime"
              label="最近修改时间">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="toggleEnable(scope.row)">{{getEnableText(scope.row.isEnable)}}</el-button>
                <el-button type="text" @click="editPosition(scope.row)">查看/编辑</el-button>
                <el-button type="text" @click="deletePosition(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            style="float: right"
            layout="prev, pager, next"
            :total="total"
            :current-page="currentPage"
            @current-change="pageChange">
          </el-pagination>
        </div>
      </div>
    </div>
    <el-dialog
      title="编辑职位"
      :visible.sync="addPositionPanelVisible"
      :close-on-click-modal="false"
      width="40%">
      <el-form ref="addPositionForm" :model="addPositionForm" :rules="rules" label-width="80px">
        <el-form-item label="职位状态">
          <el-radio-group v-model="addPositionForm.isEnable">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="名称" prop="positionName" required>
          <el-input v-model="addPositionForm.positionName" maxlength="16"></el-input>
        </el-form-item>
        <el-form-item label="职位说明">
          <el-input type="textarea" maxlength="160" v-model="addPositionForm.positionDescription"></el-input>
        </el-form-item>
        <br />
        <el-form-item>
          <el-button type="primary" @click="submitPosition">确定</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "position-management",
    data() {
      return ({
        filterStr: '',
        addPositionForm: {
          isEnable: 0,
          disabled: false,
        },
        dateValue: [],
        tableData: [],
        total: 0,
        addPositionPanelVisible: false,
        currentPage: 1,
        rules: {
          positionName: [{ required: true, message: '请填写职位名称' }]
        }
      })
    },
    mounted() {
      this.onSearch()
    },
    methods: {
      toggleEnable(data) {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });

        this.$http.post(this.GLOBAL.host, {
            method:"EmpPosition_Enable",
            data: {
              id: data.id,
              isEnable: 1 - data.isEnable,
            }
          }
        ).then(res => {
          loading.close()
          if (res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.onSearch()
          } else {
            this.$message({
              message: res.data.data,
              type: 'error'
            });
          }
        })
      },
      getEnableText(enable) {
        if (enable == 0) {
          return '禁用'
        }
        return '启用'
      },
      editPosition(data) {
        this.addPositionPanelVisible = true
        this.addPositionForm = ({
          isEnable: data.isEnable,
          disabled: true,
          id: data.id,
          positionName:data.positionName,
          positionDescription:data.positionDescription,
        })
      },
      deletePosition(id) {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });

        this.$http.post(this.GLOBAL.host, {
            method:"EmpPosition_Delete",
            data: {
              id,
            }
          }
        ).then(res => {
          loading.close()
          if (res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.onSearch()
          } else {
            this.$message({
              message: res.data.data,
              type: 'error'
            });
          }
        })
      },
      addPosition() {
        this.addPositionPanelVisible = true
        this.addPositionForm = ({
          isEnable: 0,
          disabled: false,
        })
      },
      submitPosition() {
        this.$refs['addPositionForm'].validate((valid) => {
          if (valid) {
            if (this.addPositionForm.id) {
              this.$http.post(this.GLOBAL.host, {
                  method: "EmpPosition_Update",
                  data: {
                    positionName: this.addPositionForm.positionName,
                    positionDescription: this.addPositionForm.positionDescription,
                    id: this.addPositionForm.id,
                    isEnable: this.addPositionForm.isEnable,
                  }
                }
              ).then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  });
                  this.onSearch()
                  this.addPositionPanelVisible = false
                } else {
                  this.$message({
                    message: res.data.data,
                    type: 'error'
                  });
                }
              })
            } else {
              this.$http.post(this.GLOBAL.host, {
                  method: "EmpPosition_Add",
                  data: {
                    positionName: this.addPositionForm.positionName,
                    positionDescription: this.addPositionForm.positionDescription,
                    isEnable: this.addPositionForm.isEnable,
                  }
                }
              ).then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    message: '添加成功',
                    type: 'success'
                  });
                  this.onSearch()
                  this.addPositionPanelVisible = false
                } else {
                  this.$message({
                    message: res.data.data,
                    type: 'error'
                  });
                }
              })
            }
          }
        })
      },
      pageChange(pageNum) {
        this.currentPage = pageNum;
        this.onSearch()
      },
      onSearch(lastQuery) {
        this.lastQuery = lastQuery || {
          pageNo: this.currentPage || 1,
          pageSize: 10,
          startDate: this.dateValue ? this.dateValue[0] : '',
          endDate: this.dateValue ? this.dateValue[1] : '',
          keyword: this.filterStr,
        }
        this.$http.post(this.GLOBAL.host, {
            method:"EmpPosition_List",
            query: this.lastQuery,
          }
        ).then(res => {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        })
      },
      onCancel() {
        this.$refs.addPositionForm.resetFields()
        this.addPositionPanelVisible = false
      },
    }
  }
</script>

<style lang="less">
  .position-operator {
    display: flex;
    align-items: center;
    > div {
      margin-right: 20px;
    }
    margin-bottom: 10px;
  }
</style>
